<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>

<?php

$dest = "";
if( $_FILES["uploadedFile"] != NULL) //new file is found
{
	$file_name = $_FILES["uploadedFile"]["name"];
	$tmp_file_name = $_FILES["uploadedFile"]["tmp_name"];
	
	$dest_path = $_POST["destination"];
	$dest = $dest_path."/".$file_name;
	copy($tmp_file_name, "../../../".$dest);
}

$filename_input = $_GET["filename_input"];
$dir_input = $_GET["dir_input"];

if(!isset($filename_input) || !isset($dir_input))
{
	$filename_input = $_POST["filename_input"];
	$dir_input = $_POST["dir_input"];
}

?>

<script language="javascript">

function init()
{
	var destPath = "<?php echo $dest; ?>";
	var filename_input_id = document.getElementById('filename_input');
	
	if(destPath != "") //this means it is just uploaded it..
	{
		file_input_ctrl = parent.document.getElementById(filename_input_id.value);
		file_input_ctrl.value = destPath;
		if(file_input_ctrl.onchange)
		{
			file_input_ctrl.onchange(); //just trigger the control to update..
		}
	}
}

function uploadthis(ctrl)
{
	var fileName = ctrl.value;
	
	var dir_input_id = document.getElementById('dir_input');
	var destination = parent.document.getElementById(dir_input_id.value).value;
	document.getElementById('destination').value = destination;
	ctrl.form.submit();
}
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input name="uploadedFile" title="file to upload" onchange="uploadthis(this)" type="file" />
<input id="destination" type="hidden" value="" name="destination"/>

<input id="filename_input" name="filename_input" 
		type="hidden" value="<?php echo $filename_input; ?>" />
<input id="dir_input" name="dir_input" 
		type="hidden" value="<?php echo $dir_input; ?>" />

<input id="dest_filename" type="hidden" value="<?php echo $dest; ?>" />

</form>
<script>
init();
</script>
</body>
</html>
